<?php
/**
 * ECSHOP 微信支付回调页面
 * ============================================================================
 * $Author: 凡人 QQ：644157559 $
 */
define('IN_ECS', true);
require(str_replace("/wxpay", '', dirname(__FILE__)) . '/includes/init.php');
require_once ROOT_PATH . "wxpay/lib/WxPay.Api.php";
require_once ROOT_PATH . "wxpay/lib/WxPay.Notify.php";
require_once ROOT_PATH . "wxpay/log.php";
require(ROOT_PATH . 'includes/lib_payment.php');
require(ROOT_PATH . 'includes/lib_order.php');

//初始化日志
$logHandler= new CLogFileHandler(ROOT_PATH . "wxpay/logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    die('error');
}
$payment  = get_payment('wxpay');
WxPayConfig::setValue('appid', $payment['wxpay_app_id']);
WxPayConfig::setValue('mchid', $payment['wxpay_mchid']);
WxPayConfig::setValue('key', $payment['wxpay_key']);
class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);
//		Log::DEBUG("query:" . json_encode($result));
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}
	
	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{
//		Log::DEBUG("call back:" . json_encode($data));
		$notfiyOutput = array();
		$pay_code = 'wxpay';
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}
                $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('payment') . " WHERE pay_code = '$pay_code' AND enabled = 1";
                if ($GLOBALS['db']->getOne($sql) == 0)
                {
                    $msg = $GLOBALS['_LANG']['pay_disabled'];
                    return false;
                }
                else
                {
                    $plugin_file = ROOT_PATH . 'includes/modules/payment/'.$pay_code.'.php';

                    /* 检查插件文件是否存在，如果存在则验证支付是否成功，否则则返回失败信息 */
                    if (file_exists($plugin_file)) {
                        /* 根据支付方式代码创建支付类的对象并调用其响应操作方法 */
                        include_once($plugin_file);

                        $payment = new $pay_code();
                        if($payment->respond($data)){
                            $msg = $GLOBALS['_LANG']['pay_success'];
                            return true;
                        }else{
                            $msg = $GLOBALS['_LANG']['pay_fail'];
                            return false;
                        }
                    }else{
                        $msg = $GLOBALS['_LANG']['pay_not_exist'];
			return false;
                    }
                }
		return true;
	}
}

//Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);
